<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Category_model','category');
		$this->load->model('Shopcart_model','shopcart');
	}
	
	public function add($goodsId,$num=1)
	{
		$goodsIdA = explode('_', $goodsId);
		foreach($goodsIdA as $goodsId)
		{
			$goodsId = intval($goodsId);
			if($goodsId)
				$data = $this->shopcart->add($goodsId,$num);
		}
		$return = array(
			'sum' => 0,
			'price' => 0,		
		);
		foreach($data as $v)
		{
			$return['sum'] += $v['sum'];
			$return['price'] += $v['price']*$v['sum'];
		}		
		echo json_encode($return);
	}
	
	public function setSum($goodsId,$num)
	{
		$this->shopcart->setSum($goodsId,$num);
	}
	
	public function index()
	{
		$data = $this->shopcart->getAll();		
		$this->load->view('cart/index',array('title'=>'购物车 - 艾木 iMore.cc','data'=>$data));
	}
	
	public function remove($goodsId)
	{
		//$this->shopcart->remove($goodsId);
	}
	
	public function test()
	{
	//	$this->shopcart->delete();
		$data = $this->add(1);
		print_r($data);		
	}
	
	public function get()
	{
		$shopcart = $this->input->get_cookie('Shop_Cart',array(
				'goods_id' => array(),
				'sum'        => 0,
		));
		print_r($shopcart);
	}
}

